﻿Imports System.Data.SqlClient
Imports System.Xml
Public Class frmPrincipal
    Dim NumFactura1 As Integer
    Dim FechaVenta1 As Date
    Dim CodCliente1, NombreCliente1, Equipo1, DireccionCliente1, Celular1, Telefono1, Email1, Vendedor1 As String
    Dim Pequeno As Boolean = True

    Private Sub btnBuscarDatos_Click(sender As Object, e As EventArgs) Handles btnBuscarDatos.Click
        BuscarDatosMonica()
    End Sub

    Private Sub txtNumFactura_EditValueChanged(sender As Object, e As EventArgs) Handles txtNumFactura.EditValueChanged
        If Me.txtNumFactura.BackColor = Color.LemonChiffon Then
            Me.txtNumFactura.BackColor = Color.White
        End If
    End Sub

    Private Sub BuscarDatosMonica()
        If Me.txtNumFactura.Text <> Nothing Then
            Try
                If varConexion.State = ConnectionState.Open Then
                    varConexion.Close()
                End If

                varConexion.Open()

                Dim sql As String = String.Format("SELECT a.cliente_id, b.nombre_clte, a.fecha_emision FROM factura a INNER JOIN clientes b ON a.cliente_id = b.cliente_id WHERE a.nro_factura = {0}", Me.txtNumFactura.Text)
                Dim cmd As New SqlCommand(sql, varConexion)
                Dim reader As SqlDataReader = cmd.ExecuteReader

                If reader.Read Then
                    Me.txtNombreCliente.Text = Trim(reader.Item("nombre_clte").ToString)
                    Me.txtFechaCompra.Text = Trim(FormatDateTime(reader.Item("fecha_emision").ToString, DateFormat.ShortDate))
                    Me.btnImprimirHojas.Enabled = True
                Else
                    Me.txtNombreCliente.Text = Nothing
                    Me.txtFechaCompra.Text = Nothing
                    Me.btnImprimirHojas.Enabled = False
                    MessageBox.Show("No se ha encontrado esta factura.", varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If

                varConexion.Close()

                Me.txtFechaCompra.Text = String.Format("{0}/{1}/{2}", Date.Today.Month, Date.Today.Day, Date.Today.Year)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            MessageBox.Show("Ingrese un número de factura.", varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Me.txtNumFactura.Focus()
            Me.txtNumFactura.BackColor = Color.LemonChiffon
        End If
    End Sub

    Private Sub txtNumFactura_KeyDown(sender As Object, e As KeyEventArgs) Handles txtNumFactura.KeyDown
        If e.KeyCode = Keys.Enter Then
            BuscarDatosMonica()
        End If
    End Sub

    Private Sub frmPrincipal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        rdbPaginaBlanca.Checked = True
    End Sub

    Private Sub btnImprimirHojas_Click(sender As Object, e As EventArgs) Handles btnImprimirHojas.Click
        ObtenerDatosGarantiaMonica(CInt(Me.txtNumFactura.Text))
    End Sub

    Private Sub GuardarDatosGarantiasMegastore(ByVal NumFactura As Integer, ByVal CodCliente As String,
                                               ByVal NombreCliente As String,
                                               ByVal FechaVenta As Date, ByVal Equipo As String, ByVal DireccionCliente As String,
                                               ByVal Celular As String, ByVal Telefono As String, ByVal Email As String,
                                               ByVal Vendedor As String)
        Try
            If varConexionLocal.State = ConnectionState.Open Then
                varConexionLocal.Close()
            End If
            varConexionLocal.Open()
            Dim sql As String = String.Format("INSERT INTO GarantiaMantenimiento (NumFactura, CodCliente, NombreCliente, FechaVenta, Equipo, DireccionCliente, Celular, Telefono, Email, Vendedor) " +
                                              "VALUES ({0}, '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}')", NumFactura,
                                              CodCliente, NombreCliente, Me.txtFechaCompra.Text, Equipo, DireccionCliente, Celular, Telefono,
                                              Email, Vendedor)
            Dim cmd As New SqlCommand(sql, varConexionLocal)
            cmd.ExecuteNonQuery()

            varConexionLocal.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ObtenerDatosGarantiaMonica(ByVal NumFactura As Integer)
        Try
            If varConexion.State = ConnectionState.Open Then
                varConexion.Close()
            End If
            varConexion.Open()
            Dim sql As String
            sql = String.Format("SELECT a.nro_factura, c.codigo_clte, c.nombre_clte, a.fecha_emision, e.descripcion_sub_categ, " +
                                "c.direccion1, c.telefono1, c.telefono2, c.e_mail1, f.Nombre_vendedor FROM factura a INNER JOIN factura_detalle b ON a.factura_id = b.factura_id INNER JOIN clientes c ON a.cliente_id = c.cliente_id INNER JOIN productos d ON b.producto_id = d.producto_id INNER JOIN sub_categoria_producto e ON d.Sub_categoria_id = e.sub_categoria_id INNER JOIN vendedores f ON a.vendedor_id =  f.vendedor_id WHERE (e.sub_categoria_id = '128' OR e.sub_categoria_id = '109') AND a.nro_factura = {0}", NumFactura)
            Dim cmd As New SqlCommand(sql, varConexion)
            Dim reader As SqlDataReader = cmd.ExecuteReader

            If reader.HasRows Then
                Do While reader.Read()
                    NumFactura1 = Trim(reader.Item("nro_factura"))
                    CodCliente1 = Trim(reader.Item("codigo_clte"))
                    FechaVenta1 = Trim(reader.Item("fecha_emision"))
                    NombreCliente1 = Trim(reader.Item("nombre_clte"))
                    Equipo1 = Trim(reader.Item("descripcion_sub_categ"))
                    DireccionCliente1 = Trim(reader.Item("direccion1"))
                    Celular1 = Trim(reader.Item("telefono1"))
                    Telefono1 = Trim(reader.Item("telefono2"))
                    Email1 = Trim(reader.Item("e_mail1"))
                    Vendedor1 = Trim(reader.Item("Nombre_vendedor"))

                    GuardarDatosGarantiasMegastore(NumFactura1, CodCliente1, NombreCliente1, FormatDateTime(FechaVenta1, DateFormat.ShortDate), Equipo1, DireccionCliente1, Celular1, Telefono1, Email1, Vendedor1)

                    If rdbFormato.Checked = True Then
                        Dim report As New rptGarantiaFormato
                        Dim rerport2 As New rptCertificadoMantenimiento
                        report.LlenarHojaGarantia()
                        report.ShowRibbonPreviewDialog()

                        If Equipo1 = "COMPUTADORAS DE ESCRITORIO" Then
                            rerport2.chkPC.Checked = True
                        ElseIf Equipo1 = "COMPUTADORAS PORTATILES" Then
                            rerport2.chkLaptop.Checked = True
                        End If
                        rerport2.LlenarHojaGarantia()
                        rerport2.ShowRibbonPreviewDialog()

                    ElseIf rdbPaginaBlanca.Checked = True Then
                        Dim report As New rptGarantiasNoFormato
                        Dim rerport2 As New rptCertificadoMantenimiento
                        report.LlenarHojaGarantia()
                        report.ShowRibbonPreviewDialog()

                        If Equipo1 = "COMPUTADORAS DE ESCRITORIO" Then
                            rerport2.chkPC.Checked = True
                        ElseIf Equipo1 = "COMPUTADORAS PORTATILES" Then
                            rerport2.chkLaptop.Checked = True
                        End If
                        rerport2.LlenarHojaGarantia()
                        rerport2.ShowRibbonPreviewDialog()
                    End If
                Loop
            End If

            varConexion.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnMore_Click(sender As Object, e As EventArgs) Handles btnMore.Click
        If Pequeno = True Then
            Me.Height = 500
            Pequeno = False
        Else
            Me.Height = 410
            Pequeno = True
        End If
    End Sub

    Private Sub SimpleButton1_Click(sender As Object, e As EventArgs) Handles SimpleButton1.Click
        frmGenerarReporte.ShowDialog()
    End Sub
End Class